import { defineConfig } from "vite";
import path from "node:path";
import electron from "vite-plugin-electron/simple";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import mockServer from "vite-plugin-mock-server";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => {
  return {
    envDir: path.resolve(__dirname, "./env"),
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@store": path.resolve(__dirname, "./src/store/modules"),
        "@api": path.resolve(__dirname, "./src/api"),
      },
    },
    plugins: [
      vue(),
      electron({
        main: {
          // Shortcut of `build.lib.entry`.
          entry: "electron/main.ts",
        },
        preload: {
          // Shortcut of `build.rollupOptions.input`.
          // Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
          input: path.join(__dirname, "electron/preload.ts"),
        },
        // Ployfill the Electron and Node.js API for Renderer process.
        // If you want use Node.js in Renderer process, the `nodeIntegration` needs to be enabled in the Main process.
        // See 👉 https://github.com/electron-vite/vite-plugin-electron-renderer
        renderer:
          process.env.NODE_ENV === "test"
            ? // https://github.com/electron-vite/vite-plugin-electron-renderer/issues/78#issuecomment-2053600808
              undefined
            : {},
      }),
      AutoImport({
        resolvers: [
          ElementPlusResolver(),
          // 自动导入图标组件
          IconsResolver({
            prefix: "Icon",
          }),
        ],
        dts: path.resolve(__dirname, "types/auto-imports.d.ts"),
      }),
      Components({
        resolvers: [
          ElementPlusResolver(),
          // 自动注册图标组件
          IconsResolver({
            enabledCollections: ["ep"],
          }),
        ],
        dts: path.resolve(__dirname, "types/components.d.ts"),
      }),
      //补充一个图标的导入配置
      Icons({
        autoInstall: true,
      }),
      // // mock数据
      // mockServer({
      //   logLevel: "info",
      //   urlPrefixes: ["/api/"],
      //   mockRootDir: path.relative(__dirname, "./src/api/mock"), // 模拟数据文件所放置的目录
      //   mockJsSuffix: ".mock.js", // js模拟数据文件的后缀
      //   mockTsSuffix: ".mock.ts", // js模拟数据文件的后缀
      //   noHandlerResponse404: true,
      // }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(__dirname, "src/assets/icons")],
        symbolId: "icon-[dir]-[name]", // 注意这里的icon- 前缀我在svgIcon.vue中写死了的，如果调整了记得同步改一下
        // 有特殊需求可不进行该配置
        svgoOptions: {
          // 删除一些填充的属性
          plugins: [
            {
              name: "removeAttrs",
              params: { attrs: ["class", "data-name", "fill", "stroke"] },
            },
            // 删除样式标签
            "removeStyleElement",
          ],
        },
      }),
    ],
    server: {
      /** 设置 host: true 才可以使用 Network 的形式，以 IP 访问项目 */
      host: true, // host: "0.0.0.0"
      /** 端口号 */
      port: 10000,
      /** 是否自动打开浏览器 */
      open: false,
      /** 跨域设置允许 */
      cors: true,
      /** 端口被占用时，是否直接退出 */
      strictPort: false,
      /** 接口代理 */
      proxy: {
        "/electron": {
          // target: "http://81.68.228.223:5333",
          target: "http://localhost:8888/",
          // target: "https://www.fastmock.site/mock/761e2dda2b8890ab86c928a74e8f6538",
          ws: true,
          /** 是否允许跨域 */
          changeOrigin: true,
        },
      },
    },
  };
});
